﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ePioneer_ep_pioneer_content_edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
            ddlepType.DataSource = dal.ePioneer_Type;
            ddlepType.DataTextField = "name";
            ddlepType.DataValueField = "id";
            ddlepType.DataBind();
            if (Request["id"] != null)
            {
                int id = int.Parse(Request["id"].ToString());

                CMSModel.ePioneer_Content m_epc = dal.ePioneer_Content.First(x => x.id == id);

                ddlepType.SelectedValue = m_epc.typeid.ToString();
                txt_brief.Text = m_epc.title;
                txt_content.Text = m_epc.content;
                Image1.ImageUrl = m_epc.img;
            }
        }
    }
    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnModify_Click(object sender, EventArgs e)
    {
        string imgPath = System.Configuration.ConfigurationManager.AppSettings["imgPath"].ToString();      //imgpath
        string filePath = System.Configuration.ConfigurationManager.AppSettings["filePath"].ToString();       //filepath
        if (Request["id"] != null)
        {
            try
            {
                int id = int.Parse(Request["id"].ToString());
                CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
                CMSModel.ePioneer_Content m_epc = dal.ePioneer_Content.First(x => x.id == id);
                m_epc.title = txt_brief.Text;
                m_epc.content = txt_content.Text;
                m_epc.typeid = int.Parse(ddlepType.SelectedValue);
                
                var data = flImg.FileBytes;
                string msg = "";
                string path = filePath + "pioneer/" + flImg.FileName;
               
                //imgPath = imgPath + "pioneer/" + flImg.FileName;
               
                string strImgPath = imgPath + "pioneer/" + flImg.FileName;
                m_epc.img = strImgPath;
                Deliver.AsynchronousClient.StartTranCommand(data, path, out msg);
                
                dal.SaveChanges();
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('修改成功');document.location='ep_pioneer_content.aspx'", true);
            }
            catch (Exception)
            {
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('修改失败');", true);
            }
        }
        else
        {
            CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
            CMSModel.ePioneer_Content m_epc = new CMSModel.ePioneer_Content();
            m_epc.title = txt_brief.Text;
            m_epc.content = txt_content.Text;
            m_epc.typeid = int.Parse(ddlepType.SelectedValue);
            m_epc.img = flImg.FileName;
            try
            {
                dal.AddToePioneer_Content(m_epc);
                dal.SaveChanges();
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加成功');document.location='ep_pioneer_content.aspx'", true);
            }
            catch (Exception)
            {
                
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加失败');", true);
            }
        }
    }
}